package com.liyingtao.servlet;

import com.liyingtao.bean.Admin;
import com.liyingtao.biz.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String pwd =request.getParameter("pwd");
        Admin admin=new Admin(username,pwd);
        AdminService adminservice = new AdminService();
        Admin a=adminservice.adminLogin(admin);
        if(a!=null){
            HttpSession session=request.getSession();
            session.setAttribute("admin",a);
            request.getRequestDispatcher("toMainPageServlet").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
